// pages/member/member.ts
const Navigation = require('../../utils/navigation.js')

Page({
  data: {
    currentTime: '09:50',
    selectedPackage: 'monthly', // 默认选择月会员
    agreed: false,
    packages: {
      monthly: {
        name: '月会员',
        price: '9.9积分',
        validity: '30天'
      },
      quarterly: {
        name: '季会员',
        price: '36.9积分',
        validity: '90天'
      },
      permanent: {
        name: '永久会员',
        price: '98积分',
        validity: '永久'
      }
    }
  },

  onLoad() {
    this.updateTime();
  },

  onShow() {
    this.updateTime();
  },

  updateTime() {
    const now = new Date();
    const time = now.getHours().toString().padStart(2, '0') + ':' + 
                 now.getMinutes().toString().padStart(2, '0');
    this.setData({
      currentTime: time
    });
  },

  onPackageSelect(e: any) {
    const packageType = e.currentTarget.dataset.package;
    this.setData({
      selectedPackage: packageType
    });
    
    wx.showToast({
      title: `已选择${this.data.packages[packageType as keyof typeof this.data.packages].name}`,
      icon: 'none'
    });
  },

  onAgreementToggle() {
    this.setData({
      agreed: !this.data.agreed
    });
  },

  onPurchase() {
    if (!this.data.agreed) {
      wx.showToast({
        title: '请先同意会员服务协议',
        icon: 'none'
      });
      return;
    }

    const selectedPkg = this.data.packages[this.data.selectedPackage as keyof typeof this.data.packages];
    
    wx.showModal({
      title: '确认购买',
      content: `确定要购买${selectedPkg.name}吗？\n价格：${selectedPkg.price}\n有效期：${selectedPkg.validity}`,
      success: (res) => {
        if (res.confirm) {
          wx.showToast({
            title: '购买成功！',
            icon: 'success'
          });
          
          // 这里可以添加实际的购买逻辑
          setTimeout(() => {
            this.setData({
              agreed: false,
              selectedPackage: 'monthly'
            });
          }, 2000);
        }
      }
    });
  },

  onTabTap(e: any) {
    const page = e.currentTarget.dataset.page;
    Navigation.switchTab('member', page);
  }
});